package com.duobaoyu.dby.supply.common.biz.vo.order;

import com.duobaoyu.dby.common.core.constant.CommonConstants;
import com.duobaoyu.dby.common.core.constant.StringPool;
import com.duobaoyu.dby.saas.product.common.base.serializer.BigDecimalSerializer;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 总订单详情
 *
 * @author fengshaoyu
 * @version 1.0.0
 * @date 2022/10/7 14:09
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public class OrderBlanketDetailVO implements Serializable {
	private static final long serialVersionUID = -205135569732662370L;
	/**
	 * 订单类型,real:实物订单、call:话费快充订单、flow:流量订单、coupon:卡密订单、recharge:直充订单、
	 * call_slow:话费慢充订单、electricity_fees:电费订单、electricity_fees_slow:电费慢充订单
	 * //@see com.duobaoyu.dby.channel.adapter.enums.biz.order.OrderTypeEnum
	 */
	private String orderType;
	/**
	 * 基础信息
	 */
	private BaseInfo baseInfo;
	/**
	 * 订单信息
	 */
	private List<OrderInfo> orderInfoList;
	/**
	 * 订单信息汇总
	 */
	private OrderInfoStatistics orderInfoStatistics;

	@Data
	public static class BaseInfo {
		/**
		 * 供应链交易单号
		 */
		private String supplyTradeSn;
		/**
		 * 业务订单交易号
		 */
		private String bizTradeSn;
		/**
		 * 业务来源
		 */
		private String serverName;
		/**
		 * 商户ID
		 */
		private String mchId;
		/**
		 * 用户ID
		 */
		private String userId;
		/**
		 * 创建时间
		 */
		private LocalDateTime createTime;
	}

	@Data
	public static class OrderInfo {
		/**
		 * 渠道编码
		 */
		private String channelCode;
		/**
		 * 渠道描述
		 */
		private String channelDesc;
		/**
		 * 供应链订单号
		 */
		private String orderSn;
		/**
		 * 供应链订单号父单号
		 */
		private String orderParentSn;
		/**
		 * 渠道子单号
		 */
		private String channelOrderSn;
		/**
		 * 渠道父单号
		 */
		private String channelOrderParentSn;
		/**
		 * 渠道交易单号
		 */
		private String channelTradeOrderSn;
		/**
		 * 商品成本价总额
		 */
		@JsonSerialize(using = BigDecimalSerializer.class)
		private BigDecimal totalPlatformPrice;
		/**
		 * 渠道退款金额
		 */
		@JsonSerialize(using = BigDecimalSerializer.class)
		private BigDecimal channelRefundAmount;
		/**
		 * 商品商户价总额
		 */
		@JsonSerialize(using = BigDecimalSerializer.class)
		private BigDecimal totalPlatformSellPrice;
		/**
		 * 渠道运费
		 */
		@JsonSerialize(using = BigDecimalSerializer.class)
		private BigDecimal totalChannelFreight;
		/**
		 * 渠道退款运费
		 */
		@JsonSerialize(using = BigDecimalSerializer.class)
		private BigDecimal totalChannelRefundFreight;

		/**
		 * 渠道订单号列表
		 */
		private List<String> channelOrderSnList;

		public List<String> getChannelOrderSnList() {
			return Lists.newArrayList(String.format("渠道父单号: %s", Optional.ofNullable(channelTradeOrderSn).orElse(StringPool.DASH + StringPool.DASH)),
					String.format("渠道主分销号: %s", Objects.equals(CommonConstants.ZERO.toString(),channelOrderParentSn) ? StringPool.DASH + StringPool.DASH : Optional.ofNullable(channelOrderParentSn).orElse(StringPool.DASH + StringPool.DASH)));
		}
	}

	@Data
	@AllArgsConstructor
	@NoArgsConstructor
	public static class OrderInfoStatistics {
		/**
		 * 渠道运费
		 */
		@JsonSerialize(using = BigDecimalSerializer.class)
		private BigDecimal totalChannelFreight;
		/**
		 * 渠道退款运费
		 */
		@JsonSerialize(using = BigDecimalSerializer.class)
		private BigDecimal totalChannelRefundFreight;
		/**
		 * 商品商户价总额
		 */
		@JsonSerialize(using = BigDecimalSerializer.class)
		private BigDecimal totalPlatformSellPrice;
		/**
		 * 渠道实付总额
		 */
		@JsonSerialize(using = BigDecimalSerializer.class)
		private BigDecimal totalPlatformPrice;
		/**
		 * 渠道退款总额
		 */
		@JsonSerialize(using = BigDecimalSerializer.class)
		private BigDecimal channelRefundAmount;
	}
}
